package word;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import cn.hutool.poi.word.Word07Writer;
import cn.hutool.poi.word.WordUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.*;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.element.Paragraph;
import org.junit.Test;

public class word转pdf {
    public static void main(String[] args) {
        String inputPath = "D:\\Win10Driver\\OneDrive\\桌面\\xx\\上海市临港地区人才租房补贴申请表.docx";
        String outputPath = "D:\\Win10Driver\\OneDrive\\桌面\\xx\\上海市临港地区人才租房补贴申请表.pdf";

        try(
                FileInputStream fis = new FileInputStream(inputPath);
                XWPFDocument document = new XWPFDocument(fis);//读取word
                PdfWriter writer = new PdfWriter(new FileOutputStream(outputPath));
//                PdfDocument pdfDoc = new PdfDocument(writer);
//                Document pdfDocument = new Document(pdfDoc);
        ){


        } catch(Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public  void wordtopdf() {
        String inputPath = "D:\\Win10Driver\\OneDrive\\桌面\\xx\\上海市临港地区人才租房补贴申请表.docx";
        String outputPath = "D:\\Win10Driver\\OneDrive\\桌面\\xx\\上海市临港地区人才租房补贴申请表.pdf";

        try {

            // Load the Word document
            FileInputStream fis = new FileInputStream(inputPath);
            XWPFDocument document = new XWPFDocument(fis);

            // Create PDF document
            PDDocument pdfDocument = new PDDocument();
            PDPage page = new PDPage();
            pdfDocument.addPage(page);

            // Write Word content to PDF
            PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
            for (XWPFParagraph paragraph : document.getParagraphs()) {

                for (XWPFRun run : paragraph.getRuns()) {

                    contentStream.beginText();
                    contentStream.newLineAtOffset(100, 700); // Adjust coordinates as needed
//                    contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
                    contentStream.showText(run.getText(0));
//                    contentStream.showText(run.text());
                    contentStream.endText();
                }
            }
            contentStream.close();

            // Save the PDF document
            FileOutputStream fos = new FileOutputStream(outputPath);
            pdfDocument.save(fos);
            pdfDocument.close();
            fis.close();
            fos.close();

            System.out.println("Word to PDF conversion successful.");
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

}
